// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

// Copyright (C) 2012-2013	JoostinOnline

/*
Note: If you want to use a USB Gecko, uncomment the line
#define USBGECKO
in debug.h

Do the same with
#define DOLPHIN
if you want to run it in Dolphin
*/

#include <ogc/ios.h>
#include <ogc/isfs.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include "grlib/grlib.h"
#include "init.h"
#include "postLoader.h"
#include "settings.h"
#include "title.h"

extern void __exception_setreload(int t);

static void Redraw(void) // Refresh screen
{
	GRRLIB_DrawImg(0, 0, bkg, 0, 1, 1, RGBA(255, 255, 255, 255)); // Display the background image
	return;
} // Redraw

/*
LoadIOS isn't necessary anymore.  I added the option to load a different IOS before
adding support for the vWii IOS patches.  I've left the code in case it's needed for
some reason in the future, and as an example on how to use meta.xml arguments for
anyone new to coding
*/
void LoadIOS(int argc, char **argv) {
	int i;
	for (i = 0; i < argc; i++) {
		if (!strncmp("--ios=",argv[i],6)) {
			IOS_ReloadIOS(atoi(strchr(argv[i],'=')+1));
			break;
		}
	}
}

int main(int argc, char **argv) {
	__exception_setreload(0); // In the event of a code dump, app will exit right away

	//LoadIOS(argc, argv);

	pl_QueryArgs(argc, argv);

	Initialize();
	grlib_SetRedrawCallback(Redraw, NULL);
	Settings();

	if ((present.auto_exit) && (present.exit_to))
		TITLE_LaunchAllTypes(present.exit_to);
	
	#ifndef DOLPHIN
	if (CheckIfNeek2())
	#endif
		EmuExitPrompt(); // Special exit if Wii is in *NEEK, or if you are using Dolphin
	
	Deinit();
	pl_BootPostloader();
	exit(0);  // Use exit(0) to exit a program, do not use 'return' from main()
}